package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.RedisKey;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从MySQL中查询数据同步到Redis数据库中
 * Redis数据预热
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    //spring容器启动, 并且初始化完成之后, 马上执行操作
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("--------------------vo数据初始化--------------");
        List<StrategyDetail> details = strategyDetailService.list();
        for (StrategyDetail detail : details) {
            //如果vo已经存在, 跳过, 不需要在进行初始化, 防止数据覆盖
            if (strategyStatisVOService.hasStrategyVO(detail.getId())) {
                continue;
            }

            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(detail, vo);//同属性名拷贝, 不同属性需要自己设置
            vo.setIsabroad(detail.isIsabroad());
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setTitle(detail.getTitle());
            vo.setStrategyId(detail.getId());
            vo.setThumbsupnum(detail.getThumbsupnum());
            vo.setSharenum(detail.getSharenum());
            vo.setReplynum(detail.getReplynum());
            vo.setFavornum(detail.getFavornum());
            vo.setViewnum(detail.getViewnum());
            strategyStatisVOService.setStrategyStatisVO(vo);
        }

        System.out.println("--------------------zset数据初始化--------------");

        //热门初始化
        for (StrategyDetail detail : details) {
            //判断voId是否存在zset中, 存在跳过, 不存在再初始化, 因为score会再次做加操作
            String voId = RedisKey.STRATEGY_STATIS_VO.join(detail.getId().toString());
            String zsetKey = RedisKey.STRATEGY_HOT_ZSET.getPrefix();
            if (strategyStatisVOService.isVoidExist(voId, zsetKey)) {
                continue;
            }
            //score = 阅读数 + 评论数
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisVOService.addScoreHot(detail.getId(), score);
        }

        //国内外的初始化
        for (StrategyDetail detail : details) {
            //判断voId是否存在zset中, 存在跳过, 不存在再初始化, 因为score会再次做加操作
            String voId = RedisKey.STRATEGY_STATIS_VO.join(detail.getId().toString());
            //判断国内还是国外
            String zsetKey = null;
            if (detail.isIsabroad()) {
                //国外
                zsetKey = RedisKey.STRATEGY_ABROAD_ZSET.getPrefix();
            }else {
                //国内
                zsetKey = RedisKey.STRATEGY_CHINA_ZSET.getPrefix();
            }
            if (strategyStatisVOService.isVoidExist(voId, zsetKey)) {
                continue;
            }
            //score = 阅读数 + 评论数
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisVOService.addScoreAbroadAndChina(detail.getId(), score);
        }
    }
}
